﻿using ParkSaleSDK.Adapter;
using ParkSaleSDK.Data;
using ReadOnlyParkSaleAdapter;
using static ParkSaleSDK.QuickTool.SelectStringMaker;

namespace ParkSalesRootAdapter
{
    /// <summary>
    /// 消息写入适配器
    /// </summary>
    public class MessageAdapter : WriteableDatabaseAdapter<MessageData>
    {
        protected override string appendSelelctStr(MessageData tablec)
        {
            return MakeSelectStr(TableName, c_Message.k_id, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_MessageAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(MessageData tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    /// <summary>
    /// 用户拥有的房产信息写入适配器
    /// </summary>
    public class OwnedHouseAdapter : WriteableDatabaseAdapter<OwnedHouse>
    {
        protected override string appendSelelctStr(OwnedHouse tablec)
        {
            return MakeSelectStr(TableName, c_Advertisement.k_id, tablec.ID.ToString());
        }
        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_OwnedHouseAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }
        protected override string updateSelectStr(OwnedHouse tablec)
        {
            return appendSelelctStr(tablec);
        }
    }

    /// <summary>
    /// 广告写入适配器
    /// </summary>
    public class ADAdapter : WriteableDatabaseAdapter<Advertisement>
    {
        protected override string appendSelelctStr(Advertisement tablec)
        {
            return MakeSelectStr(TableName, c_Advertisement.k_id, tablec.ID.ToString());
        }
        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_ADAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }
        protected override string updateSelectStr(Advertisement tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    /// <summary>
    /// 公司写入适配器
    /// </summary>
    public class CompanyAdapter : WriteableDatabaseAdapter<Company>
    {
        protected override string appendSelelctStr(Company tablec)
        {
            return MakeSelectStr(TableName, c_Company.k_id, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_CompanyAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(Company tablec)
        {
            return appendSelelctStr(tablec);
        }
    }

    /// <summary>
    /// 小区写入适配器
    /// </summary>
    public class LocationAdapter : WriteableDatabaseAdapter<Location>
    {
        protected override string appendSelelctStr(Location tablec)
        {
            return MakeSelectStr(TableName, c_Location.k_id, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_LocationAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(Location tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    public class OrderAdapter : WriteableDatabaseAdapter<Order>
    {
        protected override string appendSelelctStr(Order tablec)
        {
            return MakeSelectStr(TableName, c_Order.k_id, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_OrderAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(Order tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    /// <summary>
    /// 用户优惠写入适配器
    /// </summary>
    public class UserDicountAdapter : WriteableDatabaseAdapter<UserDiscount>
    {
        public UserDicountAdapter(string server, string database, string username, string password, int port)
        {
            Init(server, database, c_Discount.Table, username, password, port);
        }
        public UserDicountAdapter()
        {
        }
        protected override string appendSelelctStr(UserDiscount tablec)
        {
            return MakeSelectStr(TableName, c_Discount.k_ID, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_UserDiscountAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(UserDiscount tablec)
        {
            return appendSelelctStr(tablec);
        }
    }

    /// <summary>
    /// 车位写入适配器
    /// </summary>
    public class ParkTableAdapter : WriteableDatabaseAdapter<Park>
    {
        public ParkTableAdapter()
        {
        }

        public ParkTableAdapter(string server, string database, string username, string password, int port)
        {
            Init(server, database, "Park", username, password, port);
        }
        protected override string appendSelelctStr(Park tablec)
        {
            return MakeSelectStr(TableName, c_Parking.k_ID, tablec.ID.ToString());
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new readOnly_ParkTableAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }

        protected override string updateSelectStr(Park tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    /// <summary>
    /// 产权写入适配器
    /// </summary>
    public class ParkRightTableAdapter : WriteableDatabaseAdapter<Rights>
    {
        public ParkRightTableAdapter()
        {
        }

        public ParkRightTableAdapter(string server, string database, string username, string password, int port)
        {
            Init(server, database, "propertyRight", username, password, port);
        }
        protected override string appendSelelctStr(Rights tablec)
        {
            return MakeSelectStr(TableName, c_Rights.k_ID, tablec.ID.ToString());
        }
        protected override string deleteSelectStr(int mode, string value)
        {
            return ((ReadableDatabaseAdapter)(new ReadOnlyParkSaleAdapter.readOnly_ParkRightAdapter().Init(this.DatabaseConnect, this.TableName))).FindSelectStr(mode, value);
        }


        protected override string updateSelectStr(Rights tablec)
        {
            return appendSelelctStr(tablec);
        }
    }
    /// <summary>
    /// 用户读写适配器
    /// </summary>
    public class UserTableAdapter : WriteableDatabaseAdapter<User>, IDatabaseReadable
    {
        public UserTableAdapter(string server, string database, string username, string password, int port)
        {
            Init(server, database, "User", username, password, port);
        }

        protected override string appendSelelctStr(User tablec)
        {
            return MakeSelectStr(TableName, c_User.k_ID, tablec.ID);
        }

        protected override string deleteSelectStr(int mode, string value)
        {
            return FindSelectStr(mode, value);
        }

        string FindSelectStr(int mode, string value)
        {
            switch (mode)
            {
                default:
                    return MakeSelectStr(TableName);
                case c_User.m_id:
                    return MakeSelectStr(TableName, c_User.k_ID, value);
                case c_User.m_username:
                    return MakeSelectStr(TableName, c_User.k_UserName, value);
                case c_User.m_Phone:
                    return MakeSelectStr(TableName, c_User.k_Phone, value);
                case c_User.m_Location:
                    return MakeSelectStr(TableName, c_User.k_Location, value);
                case c_User.m_House:
                    return MakeSelectStr(TableName, c_User.k_House, value);
                case c_User.m_Number:
                    return MakeSelectStr(TableName, c_User.k_Number, value);
                case c_User.m_Password:
                    return MakeSelectStr(TableName, c_User.k_Password, value);
            }
        }

        protected override string updateSelectStr(User tablec)
        {
            return MakeSelectStr(TableName, c_User.k_ID, tablec.ID);
        }

        public T[] Find<T>(int mode, string value) where T : new()
        {
            var m = GetMySqlManager(FindSelectStr(mode, value));
            return m.Find<T>().ToArray();
        }
    }
}
